package com.example.administrator.financialmanager;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.financialmanager.dao.FlagDao;
import com.example.administrator.financialmanager.model.Tb_flag;

public class FlagManageActivity extends AppCompatActivity {

    EditText flag_manage_input;// 创建EditText对象
    Button flag_manage_update_btn, flag_manage_delete_btn,flag_manage_cancel_btn;// 创建两个Button对象
    String strid;// 创建字符串，表示便签的id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flag_manage);// 设置布局文件
        flag_manage_input = (EditText) findViewById(R.id.flag_manage_input);// 获取便签文本框
        flag_manage_update_btn = (Button) findViewById(R.id.flag_manage_update_btn);// 获取修改按钮
        flag_manage_delete_btn = (Button) findViewById(R.id.flag_manage_delete_btn);// 获取删除按钮
        flag_manage_cancel_btn = (Button) findViewById(R.id.flag_manage_cancel_btn);// 获取取消按钮

        Intent intent = getIntent();// 创建Intent对象
        Bundle bundle = intent.getExtras();// 获取便签id
        strid = bundle.getString(DataManageHomeActivity.FLAG);// 将便签id转换为字符串
        final FlagDao flagDao = new FlagDao(FlagManageActivity.this);// 创建FlagDAO对象
        flag_manage_input.setText(flagDao.find(strid).getFlag());// 根据便签id查找便签信息，并显示在文本框中

        flag_manage_update_btn.setOnClickListener(new View.OnClickListener() {// 为修改按钮设置监听事件
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Tb_flag tb_flag = new Tb_flag();// 创建Tb_flag对象
                tb_flag.setid(strid);// 设置便签id
                tb_flag.setFlag(flag_manage_input.getText().toString());// 设置便签值
                flagDao.update(tb_flag);// 修改便签信息
                // 弹出信息提示
                Toast.makeText(FlagManageActivity.this, "〖便签数据〗修改成功！",
                        Toast.LENGTH_SHORT).show();

            }
        });

        flag_manage_delete_btn.setOnClickListener(new View.OnClickListener() {// 为删除按钮设置监听事件
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                flagDao.detele(strid);// 根据指定的id删除便签信息
                Toast.makeText(FlagManageActivity.this, "〖便签数据〗删除成功！",
                        Toast.LENGTH_SHORT).show();
            }
        });
        flag_manage_cancel_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
